package com.songcha.module_mine.ui.activity.browse_history

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.songcha.library_base.mvvm.base.BaseMvvmActivity
import com.songcha.library_business.bean.book.BookBean
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.module_mine.R
import com.songcha.module_mine.databinding.MineActivityBrowseHistoryBinding
import com.songcha.module_mine.structs.BrowseHistoryItem
import com.songcha.module_mine.ui.adapter.BrowseHistoryAdapter
import com.songcha.module_mine.ui.adapter.MessageNotifyAdapter

@Route(path="/mine/BrowseHistoryActivity")
class BrowseHistoryActivity:BaseMvvmActivity<BrowseHistoryViewModel,MineActivityBrowseHistoryBinding>(),OnItemChildClickListener {
    private var mAdapter: BrowseHistoryAdapter?=null

    override fun getLayoutId():Int{
        return R.layout.mine_activity_browse_history
    }


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        val majiaName = MajiaBaoUtil.getMajiaName()
        if(majiaName == "douyue"){
            databinding.mineHbv.setTitle("浏览历史")
        }else if(majiaName == "beike"){
            databinding.mineHbv.setTitle("看过")
        }
    }

    override fun initObserver() {
        viewModel.historyList.observe(this){data->
            initRcv(data)
        }
    }

    override fun isInitViewGetData(): Boolean {
        return false
    }

    override fun onGetData() {
        viewModel.getBrowseHistoryList()
    }

    override fun onResume() {
        super.onResume()
        onGetData()
    }

    override fun isCalculateHearBarHeight(): Boolean {
        return true
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    private fun initRcv(list: MutableList<BrowseHistoryItem>){
        databinding.mineRcvBrowseHistory.apply{
            layoutManager= LinearLayoutManager(context)
            mAdapter= BrowseHistoryAdapter(list)
            mAdapter!!.setOnItemChildClickListener(this@BrowseHistoryActivity)
            adapter=mAdapter
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when(view.id){
            R.id.mine_btn_read->{
                val item=viewModel.historyList.value!![position]
                val book=BookBean().apply {
                    id=item.bookId
                    thumb=item.thumb
                    bookName=item.bookName
                    author=item.author
                    textCount=item.textCount
                }
                BusinessJumpUtil.toBookReader(book)
            }
        }
    }

}